<template>
    <el-card>
        
        <el-form :inline="true">
            <el-form-item>
                <el-input v-model="query.label" placeholder="请输入名称" :clearable="true"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button icon="el-icon-search" type="primary" @click="search">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button icon="el-icon-refresh-right" @click="handleRest">重置</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="test">弹窗</el-button>
            </el-form-item>
        </el-form>
        <r-table :data="tableData" :columns="columns" :loading="loading">
            <template slot="recommend" slot-scope="scope">
                {{ scope.row.recommend }}
                <el-switch
                    v-show="scope.row.status"
                    active-value="1"
                    inactive-value="0"
                    @change="handleRecommend(scope.row)"
                    >
                </el-switch>
            </template>
            <template slot="actions" slot-scope="scope">
                <div>
                    <el-button 
                    :ref="'install_btn' + scope.row.id" 
                    type="text" 
                    v-show="!scope.row.status"
                    :loading="scope.row.installing"
                    @click="install(scope.row)">安装</el-button>
                    
                    <div v-show="scope.row.status">
                        <el-button type="text" >设置</el-button>
                        <el-divider direction="vertical"></el-divider>
                        <el-button type="text" style="margin-left: 0px" @click="uninstall(scope.row)">卸载</el-button>
                    </div>
                </div>
            </template>
            <template slot="status" slot-scope="data">
                <div style="display: none">{{ data.row.running }}</div>
                <div v-if="data.row.status">
                    <el-tooltip 
                    v-if="data.row.running"
                    class="item"
                    effect="dark"
                    content="正在运行"
                    placement="top">
                          <i class="el-icon-video-play"
                          style="font-size: 26px;color: #67C23A;cursor: pointer;" 
                          @click="handleStop(data.row)"></i>
                    </el-tooltip>
                    <el-tooltip 
                    v-else
                    class="item"
                    effect="dark"
                    content="暂未运行"
                    placement="top">
                        <i class="el-icon-video-pause"
                        style="font-size: 26px;color: red;cursor: pointer;" 
                        @click="handleStart(data.row)"></i>
                    </el-tooltip>
                </div>


            </template>
            <template slot="position" slot-scope="data">
                {{ data.row.position }}
            </template>
        </r-table>
    </el-card>
</template>

<script>
    import { getSofts, installSoft, uninstallSoft, recommendSoft, startSoft, stopSoft } from "@/api/soft"
    import Vue from 'vue'
    import Database from '@/views/database/Database'
    Vue.component('database', Database)
    export default {
        name: 'soft',
        data () {
            return {
                loading: false,
                total: 100,
                query: {
                    page: 1
                },
                tableData: [

                ],
                columns: [
                    {
                        value: 'label',
                        label: '名称',
                        width: '200',
                    },
                    {
                        value: 'explain',
                        label: '说明',
                        width: '600',
                    },
                    {
                        value: 'position',
                        label: '位置',
                        scope: 'position'
                    },
                    {
                        value: 'status',
                        label: '状态',
                        scope: 'status'
                    },
                    // {
                    //     value: 'recommend',
                    //     scope: 'recommend',
                    //     label: '首页显示'
                    // },
                    {
                        scope: 'actions',
                        label: '操作'
                    }
                ]
            }
        },
        methods: {
            handleStart(soft) {
                startSoft(soft.name, soft.version).then(res => {
                    soft.running = 1
                    this.$message.success("开启成功")
                    console.log(res)
                })
            },
            handleRecommend (soft) {
                console.log(soft)
                let recommend = soft.recommend ? 1 : 0
                recommendSoft(soft.name, soft.version, recommend)
            },
            install (soft) {
                console.log(soft.name, soft.version)
                this.$refs['install_btn' + soft.id].loading = true
                installSoft(soft.name, soft.version).then(res => {
                    console.log(res)
                    this.$refs['install_btn' + soft.id].loading = false
                    soft.status = 1
                    this.$message.success('安装成功')
                    
                })
            },
            handleStop (soft) {
                stopSoft(soft.name, soft.version).then(res => {
                    console.log(res)
                    this.$message.success('停止成功')
                    soft.running = 0
                })
            },
            loadData () {
                this.loading = true
                getSofts().then(res => {
                    this.loading = false
                    this.tableData = res.data
                })
            },
            search () {
                if (this.query.label == '') {
                    this.loadData()
                    return
                }
                let result = this.tableData.filter(v => {
                    return v.label.search(new RegExp(this.query.label, "i")) >= 0
                })
                this.tableData = result
            },
            handleRest () {
                this.query.label = ''
                this.loadData()
            },
            uninstall (item) {
                this.$confirm("确认要卸载" + item.label + "吗?", "提示", {
                    confirmButtonText: '确定',
                    type: 'warning'
                }).then(() => {
                    uninstallSoft(item.name, item.version).then(() => {
                        item.status = 0
                        this.$message.success(item.label + "卸载成功")
                    })
                })
            },
            test () {
                this.$modal('database', '数据库列表')
            }
        },
        created () {
            this.loadData()
        },
        watch: {
            'query.page' (p) {
                console.log(p)
            }
        }
    }
</script>